// ActionScript file
import com.adobe.serialization.json.JSON;

import mx.collections.ArrayCollection;
import mx.utils.ArrayUtil;

public var clienteId:String="";
public var servicio:String="";
[Bindable] public var valores:ArrayCollection = new ArrayCollection();
[Bindable] public var anioList:ArrayCollection = new ArrayCollection();

private function callWebService(anio:String):void{
	
	
		
		clienteId = Application.application.parameters.id;
		servicio = Application.application.parameters.servicio;
		
		var loader:URLLoader = new URLLoader();
		var header1:URLRequestHeader = null;
		header1 = new URLRequestHeader("pragma", "no-cache");
		var request:URLRequest = new URLRequest(servicio);
		request.requestHeaders.push(header1);
		request.method = URLRequestMethod.POST;
		
       
       
       	request.data = '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{"cliente":"'+ clienteId +'","anio":"' + anio + '"}</string>';
   
		request.contentType = "text/xml";
		loader.addEventListener(Event.COMPLETE, onLoaded);
		loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		
		try{
			loader.load(request);
		}catch(error:ArgumentError){
			trace("An ArgumentError has occurred.");
			Mask.close(); 
		}catch (error:SecurityError) 
		{ 
		    trace("A SecurityError has occurred."); 
		    Mask.close();
		}
		
		/*
		 try 
{ 
    loader.load(request); 
} 
catch (error:ArgumentError) 
{ 
    trace("An ArgumentError has occurred."); 
} 
catch (error:SecurityError) 
{ 
    trace("A SecurityError has occurred."); 
} 
		 */
}

private function onLoaded(e:Event):void {
	
	var repuesta:String=e.target.data;
	repuesta = repuesta.replace("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">","");
	repuesta = repuesta.replace("</string>","");
						
	var responseJSON:Object =  JSON.decode(repuesta);
	
		//REGIONES
	var data:Array=responseJSON.data;
	valores.source=data;
	Mask.close();
	//var regiones:Array=responseJSON.d;
	
	///valores=new ArrayCollection(ArrayUtil.toArray(responseJSON.d));
	
	 
}

private function ioErrorHandler(event:IOErrorEvent):void
            {
            	//mx.controls.Alert.show("Error cargando datos, verificar servidor.");
                trace("ioErrorHandler: " + event);
                Mask.close();
            }